<?php
/**
 * Task Plugin, tasks component: lists tasks of a given namespace
 * 
 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author   Esther Brunner <wikidesign@gmail.com>
 */

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');

require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_object_objectlist extends DokuWiki_Syntax_Plugin {

  function getInfo(){
    return array(
      'author' => 'Martin Neitz',
      'email'  => 'mneitz@gmail.com',
      'date'   => '2007-02-01',
      'name'   => 'object list component',
      'desc'   => 'Lists objects of a given namespace',
      'url'    => '',
    );
  }

  function getType(){ return 'substition'; }
  function getPType(){ return 'block'; }
  function getSort(){ return 306; }
  
  function connectTo($mode){
    $this->Lexer->addSpecialPattern('\{\{objectlist>.+?\}\}', $mode, 'plugin_object_objectlist');
  }

  function handle($match, $state, $pos, &$handler){
    global $ID;
    
    $match = substr($match, 13, -2); // strip {{objectlist> from start and }} from end
    list($match, $flags) = explode('&', $match, 2);
    $flags = explode('&', $flags);
    list($match, $refine) = explode(' ', $match, 2);
    list($ns, $objspc) = explode('?', $match, 2);
    
    if (($ns == '*') || ($ns == ':')) $ns = '';
    elseif ($ns == '.') $ns = getNS($ID);
    else $ns = cleanID($ns);
    
    return array($ns, $objspc, $flags, $refine);
  }

  function render($mode, &$renderer, $data){
    global $conf;
    
    
    list($ns, $objspc, $flags, $refine) = $data;
    
    if ($my =& plugin_load('helper', 'object')) $pages = $my->getObjects($ns, NULL, $objspc, $this->specificationPath());
 
        
    // use tag refinements?
    if ($refine){
      if (plugin_isdisabled('tag') || (!$tag = plugin_load('helper', 'tag'))){
        msg('The Tag Plugin must be installed to use tag refinements.', -1);
      } else {
        $pages = $tag->tagRefine($pages, $refine);
      }
    }
    

    if ($mode != 'xhtml') return true;
    $renderer->info['cache'] = false;
        
    // prepare pagination
    $c = count($pages);
    if ($c > $conf['recent']){
      $numOfPages = ceil($c / $conf['recent']);
      $first = $_REQUEST['first'];
      if (!is_numeric($first)) $first = 0;
      $currentPage = round($first / $conf['recent']) + 1;
      $pages = array_slice($pages, $first, $conf['recent']);
    }
    
    if ($mode == 'xhtml'){
      
      // prevent caching to ensure content is always fresh
      $renderer->info['cache'] = false;
      
      
      // let Pagelist Plugin do the work for us
      if (plugin_isdisabled('pagelist')
        || (!$pagelist = plugin_load('helper', 'pagelist'))){
        msg('The Pagelist Plugin must be installed for task lists.', -1);
        return false;
      }
      
      // show view filter popup if not 
      if ($select) $renderer->doc .= $this->_viewMenu($filter);
      
      // prepare pagelist columns
//      $pagelist->header['page'] = $this->getLang('_name_');
      $pagelist->header['page'] = "Kontakt";
      
      $pagelist->header['obj_country'] = "Country";
      $pagelist->header['obj_pcode'] = "ZIP";
      $pagelist->header['obj_street'] = "Stadt";
      
      
//     switch of the column 'user'      
      $pagelist->column['user'] = false;
      $pagelist->column['tags'] = false;
      $pagelist->column['date'] = false;
      $pagelist->setFlags($flags);
      $pagelist->addColumn('object', 'obj_country');
      $pagelist->addColumn('task', 'obj_pcode');
      $pagelist->addColumn('object', 'obj_street');
      
      
      
      
      // output list
      $pagelist->startList();
      foreach ($pages as $page){
        $pagelist->addPage($page);
      }
      $renderer->doc .= $pagelist->finishList();
      $renderer->doc .= $this->_paginationLinks($numOfPages, $currentPage, $filter);      
      
      return true;
      
    // for metadata renderer
    } elseif ($mode == 'metadata'){
      foreach ($pages as $page){
        $renderer->meta['relation']['references'][$page['id']] = true;
      }
      
      return true;
    }
    return false;
  }
  

  /**
   * Returns pagination links if more than one page
   */
  function _paginationLinks($num, $cur, $filter){
    global $ID, $conf;
    
    if (!is_numeric($num) || ($num < 2)) return '';
    
    $ret = array();
    for ($i = 1; $i <= $num; $i++){
      if ($i == $cur) $ret[] = '<strong>'.$i.'</strong>';
      else $ret[] = '<a href="'.wl($ID, array('first' => $conf['recent'] * ($i - 1),
        'filter' => $filter)).'" class="wikilink1" alt="'.$i.'">'.$i.'</a>';
    }
    return '<div class="centeralign">'.DOKU_LF.
      DOKU_TAB.join(' | ', $ret).DOKU_LF.
      '</div>'.DOKU_LF;
  }
  
  function specificationPath(){
    /**
     * If you subclass this action e.g. for the addContakt syntax you can overide 
     * the path of the specification e.g. to the contact plugin folder.
     */      
    
    return DOKU_INC.'lib/plugins/object/objspc' ;
  }
  
  
}

//Setup VIM: ex: et ts=4 enc=utf-8 :
